/**
  ******************************************************************************
  * File Name          : ModbusSlave.h
  * Description        : 本文档包含easy_modbus协议的宏定义、变量声明、函数声明等 
  *
  ******************************************************************************
  *
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/

#ifndef __MODBUSSLAVE__
#define __MODBUSSLAVE__


/* Includes ------------------------------------------------------------------*/

#include "alldefine.h"

#include "string.h"


#ifdef MODBUS_HANDLE_CRC_ENABLE

#include "CrcCheck.h"

#endif

/*E_MBUS资源类型宏定义------------------------------------------*/
#define		E_MBUS_SWITCH		GPIO_PinState
#define		E_MBUS_REG			uint16_t
#define		E_MBUS_DAT_SIZE	uint16_t
#define		E_MBUS_CMD 			uint8_t
#define		E_MBUS_ID 			uint8_t
#define		E_MBUS_DATA 		uint16_t

/*数据处理BUF类型宏定义------------------------------------------*/
#define		E_MBUS_BUF_DATA uint8_t
#define		E_MBUS_BUF_SIZE uint16_t


/*IO状态宏定义-----------------*/
#define E_MBUS_Y_PIN_SET		GPIO_PIN_SET
#define E_MBUS_Y_PIN_RESET	GPIO_PIN_RESET

#define E_MBUS_X_PIN_SET		GPIO_PIN_RESET
#define E_MBUS_X_PIN_RESET	GPIO_PIN_SET

/*资源数量宏定义------------------------------------------*/
#define	E_MBUS_X_SIZE		2
#define	E_MBUS_Y_SIZE		2
#define	E_MBUS_RREG_SIZE	2
#define	E_MBUS_WRREG_SIZE0	300
#define	E_MBUS_WRREG_SIZE1	20

#define E_MBUS_WRREG_ADDR0	0x0000
#define E_MBUS_WRREG_ADDR1	1000



#define WRITE_ENABLE	1
#define WRITE_DISABLE	0

#define WRITE_ADDR	167

/* Exported typedef ------------------------------------------------------------------*/


/*
**MODBUS异常码枚举
**/

typedef enum
{
  E_MBUS_OK = 0,
  E_MBUS_ERR_CMD,
  E_MBUS_ERR_ADDR,
	E_MBUS_ERR_DATE,
  E_MBUS_ERR_SIZE,
	E_MBUS_ERR_CRC=-1,
	E_MBUS_ERR_ID=-2,
  E_MBUS_ERROR_OTHER=-3
}E_MBUS_StatusTypeDef;



/*
**MODBUS功能码
**/

typedef enum
{
  MBUS_READ_Y = 0X01,
  MBUS_READ_X=0X02,
  MBUS_READ_WRREG=0X03,
  MBUS_READ_RREG=0X04,
  MBUS_Y_WriteOnePin=0x05,
  MBUS_WRREG_WriteOneReg=0x06,
  MBUS_Y_Write=0x0f,
  MBUS_WRREG_Write=0x10
}E_MBUS_CMD_TypeDef;





/*
**E_MBUS资源结构体
**
**mbus_x[E_MBUS_X_SIZE]:						输入开关资源
**mbus_y[E_MBUS_Y_SIZE]：						输出继电器资源
**mbus_rreg[E_MBUS_RREG_SIZE]：			只读寄存器资源
**mbus_wrreg0[E_MBUS_WRREG_SIZE0]：	可读写寄存器资源段0
**mbus_wrreg1[E_MBUS_WRREG_SIZE1]：	可读写寄存器资源段1
**mbus_id														本机MODBUSSLAVE ID
**
-------------------------*/

typedef struct
{
	E_MBUS_SWITCH		mbus_x[E_MBUS_X_SIZE];
	
	E_MBUS_SWITCH		mbus_y[E_MBUS_Y_SIZE];
	
	E_MBUS_REG			mbus_rreg[E_MBUS_RREG_SIZE];
	
	E_MBUS_REG			mbus_wrreg0[E_MBUS_WRREG_SIZE0];
	
	E_MBUS_REG			mbus_wrreg1[E_MBUS_WRREG_SIZE1];
	
	E_MBUS_ID				mbus_id;
	
}E_MBUS_DataTypeDef;



/*
**E_MBUS数据处理结构体----
**接收数据指针和返回数据指针，ID,CMD,等参数-
**
**
------------------------------*/
typedef struct
{
	E_MBUS_BUF_DATA		*m_bus_rxbuf;
	E_MBUS_BUF_SIZE		m_bus_rxsize;
	
	E_MBUS_BUF_DATA		*m_bus_txbuf;
	E_MBUS_BUF_SIZE		m_bus_txsize;
	
	
	E_MBUS_ID					mbus_id;
	E_MBUS_CMD				mbus_cmd;
	E_MBUS_REG				mbus_start;
	E_MBUS_REG				mbus_size;
	E_MBUS_DAT_SIZE		mbus_data_size;
	
}E_MBUS_BufDataTypeDef;


typedef struct
{
	uint8_t 	buf[512];
	uint16_t 	size;
	
}DATA_BUF_DEF;

/* Function ------------------------------------------------------------------*/

extern E_MBUS_BufDataTypeDef 		E_MBUS_BUF_DATA_ALL;
extern E_MBUS_DataTypeDef 			E_MBUS_DATA_ALL;


extern E_MBUS_StatusTypeDef E_MBUS_HANDLE(E_MBUS_BufDataTypeDef *e_mbus_bufdata);

extern void  E_MBUS_INIT(void);

#endif /*__ pinoutConfig_H */

/**
  * @}
  */

/**
  * @}
  */

/************************ (C) COPYRIGHT INLEEQ *****END OF FILE****/
